<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: evsporsmal.php 6 2005-07-11 23:14:47Z say $
 */
 
include("../db.php");
include("auth.php");
include("top.php");
include("evskjemainclude.php");
evskjemameny();

if ($_REQUEST['Utfyllende'])
	$utf=1;
else
	$utf=0;

if ($_REQUEST['action'] == 'new') {
	$sql="INSERT INTO r_evsporsmal (Nummer, Tekst, Utfyllende, Skala, LaivID) VALUES ";
	$sql.= "({$_REQUEST['Nummer']}, '{$_REQUEST['Tekst']}', {$utf}, {$_REQUEST['Skala']}, $laivid)";
	mysql_query($sql);
} elseif ($_REQUEST['action'] == 'update') {
	if ($_REQUEST['subby'] == 'Lagre') {
		$sql="UPDATE r_evsporsmal SET ";
		$sql.= "Nummer={$_REQUEST['Nummer']}, Tekst='{$_REQUEST['Tekst']}', Utfyllende={$utf}, Skala={$_REQUEST['Skala']}";
		$sql .= " WHERE ID={$_REQUEST['id']}";
		mysql_query($sql);
	} else {
		$id=$_REQUEST['id'];
		$sql="DELETE FROM r_evsporsmal WHERE ID=$id";
		mysql_query($sql) or die(mysql_error());
		$sql="DELETE FROM r_evbesvarelser WHERE SporsmalID=$id";
		mysql_query($sql) or die(mysql_error());
	}
}

?>
<h1>Evalueringssp�rsm�l</h1>
<?
$sql="SELECT Nummer, r_evsporsmal.Tekst, Utfyllende, Skala, r_evsporsmal.ID FROM r_evsporsmal LEFT JOIN r_evskalaer ON r_evsporsmal.Skala = r_evskalaer.ID WHERE LaivID=$laivid ORDER BY Nummer";
$spmalene = mysql_query($sql) or die(mysql_error());

startTable();

while ($spm = mysql_fetch_row($spmalene)) {
	$data[0] = "<input name=\"Nummer\" value=\"$spm[0]\" size=\"4\">";
	$data[1] = "<textarea name=\"Tekst\">{$spm[1]}</textarea>";
	if ($spm[2])
		$sel = " CHECKED";
	else
		$sel = "";
	$data[2] = "<input type=\"checkbox\" value=\"1\" name=\"Utfyllende\"$sel>";
	$data[3] = getSkalaSelect($spm[3]);
	
	row($spm[4], $data);
}

endTable();

echo "<h2>Nytt sp�rsm�l</h2>\n";
startTable();
?>
<form method="post" action="evsporsmal.php">
<input type="hidden" name="action" value="new">
<tr>
	<th>Nummer</th>
	<td><input name="Nummer" size="4"></td>
</tr>
<tr>
	<th>Tekst</th>
	<td><textarea name="Tekst"></textarea></td>
</tr>
<tr>
	<th>Svar med tekstboks?</th>
	<td><input type="checkbox" value="1" name="Utfyllende"></td>
</tr>
<tr>
	<th>Velg skala som skal brukes</th>
	<td><?=getSkalaSelect()?></td>
</tr>
<tr>
	<th>Lagre</th>
	<td><input type="submit" value="Lagre">
</tr>
</form>


<?
endTable();

include("bottom.php");

function startTable() {
	echo "<table class=\"gridded\">\n";	
}

function endTable() {
	echo "</table>\n";
}

function generalRow($cells, $header=false) {
	echo "\t<tr>\n";
	foreach ($cells as $cell) {
		if ($header)
			echo "\t\t<th>$cell</th>\n";
		else
			echo "\t\t<td>$cell</td>\n";
	}
	echo "\t</tr>\n";
}

function row($id, $cells, $header=false) {
	echo "\t<form method=\"post\" action=\"evsporsmal.php\">\n";
	echo "\t<input type=\"hidden\" name=\"action\" value=\"update\">\n";
	echo "\t<input type=\"hidden\" name=\"id\" value=\"$id\">\n";

	$cells[] = "<input type=\"submit\" name=\"subby\" value=\"Lagre\">";
	$cells[] = "<input type=\"submit\" name=\"subby\" value=\"Slette\">";

	generalRow($cells, $header);
	
	echo "</form>";
}

function getSkalaArray() {
	$sql="SELECT * FROM r_evskalaer ORDER BY Navn";
	$skalaer=mysql_query($sql);
	while ($skala=mysql_fetch_row($skalaer))
		$retur[$skala[0]] = $skala[1];
	
	return $retur;
}

function getSkalaSelect($sel=0, $emptyoption=true) {
	$skalaer=getSkalaArray();
	$txt="<select name=\"Skala\">\n";
	if ($emptyoption) 
		$txt .="\t<option value=\"0\">[ingen skala]</option>\n";

	foreach ($skalaer as $id=>$navn) {
		if ($id == $sel)
			$add=" SELECTED";
		else
			$add="";
		$txt.="\t<option value=\"$id\"$add>$navn</option>\n";
	}
	$txt.="</select>\n";
	return $txt;
}
?>
